package com.test.zhangxin.jianpin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.test.zhangxin.jianpin.R;

/**
 * Created by admin on 2017/6/21.
 */

public class RvHorAdapter extends RecyclerView.Adapter<RvHorAdapter.RvHolder> {
    private Context mContext;
    private int[] mImgs;

    public RvHorAdapter(Context context, int[] imgs) {
        mContext = context;
        mImgs = imgs;
    }

    @Override
    public RvHorAdapter.RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_hor, parent, false);
        RvHolder holder = new RvHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RvHorAdapter.RvHolder holder, final int position) {
        holder.bindData(position);
    }

    @Override
    public int getItemCount() {
        return mImgs.length;
    }

    public class RvHolder extends RecyclerView.ViewHolder {

        private final ImageView mImg;

        public RvHolder(View view) {
            super(view);
            mImg = (ImageView) view.findViewById(R.id.img);
            mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null)
                        mListener.onItemClick(v, getLayoutPosition());
                }
            });
        }

        public void bindData(int position) {
            mImg.setImageResource(mImgs[position]);
        }

    }

    private OnItemClickListener mListener;

    /**
     * item的点击事件回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
}
